<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <meta http-equiv='X-UA-Compatible' content='ie=edge'>
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        textarea {
            width: 300px;
            height: 100px;
            border-radius: 10px;
            resize: none;
            outline: none;
            text-indent: 10px;
            padding-top: 10px;
            border: none;
            margin-left: 10px;
            background: #f5f5f6;
        }

        button {
            width: 80px;
            height: 30px;
            background: #4eb0f2;
            line-height: 30px;
            border-radius: 15px;
            border: none;
        }

        li {
            list-style: none;
            width: 380px;
            height: 20px;
            margin-top: 10px;
            height: 80px;
            border-bottom: 1px dashed #999;
            color: #666;
        }

        span {
            display: block;
            height: 20px;
            line-height: 20px;
            font-size: 12px;
        }

        div {
            width: 60px;
            height: 60px;
            float: left;
            margin-right: 10px;
        }

        img {
            height: 60px;
            width: 60px;
            border-radius: 50%;
            float: left;
        }

        p {
            width: 300px;
            height: 50px;
            float: left;
        }

        a {
            text-decoration: none;
            float: right;
        }
    </style>
</head>

<body>
    <textarea name="" id="text" placeholder="请输入神评论"></textarea>
    <button>提交</button>
    <ul>
        <!-- <li>
            <div>
                <img src="img/1.png" alt=""> 
            </div>
            <p>111111</p>
            <span>时间</span>
            <a href="javascript:;">删除</a>
        </li>  -->
    </ul>
</body>

</html>
<script>
    /*
     思路：点击按钮之后，就动态创建一个li，添加到ul 里面。
       
    */
    var text = document.querySelector('#text')
    var btn = document.querySelector('button')
    var uls = document.querySelector('ul')
    // console.log(text.value);
    // console.log(text.innerHTML);
    //存放图片路径
    var imgs = ['img/1.png', 'img/2.png', 'img/3.png', 'img/4.png']
    btn.onclick = function () {
        if (!text.value == '') { // 判断内容不能为空
        var index = parseInt(Math.random() * imgs.length) // 随机索引
        //创建结构
        uls.innerHTML += `
                <li>
                    <div>
                        <img src="${imgs[index]}" alt=""> 
                    </div>
                    <p>${text.value}</p>
                    <span>${'发布时间：' + time}</span>
                    <a href="javascript:;">删除</a>
                </li> 
            `
        text.value = '' // 清空输入文本域
        } else { 
            alert('请输入内容')
        }
    }
    //删除
    uls.onclick = function (e) {
        var e = e || window.event
        var target = e.target
        if (target.nodeName == 'A')
            // 删除的是 当前a所在的li
            uls.removeChild(target.parentNode)        
    }
    
    //创建时间
    var date = new Date()
    var year = date.getFullYear()
    var month = date.getMonth() + 1;
    month < 10 ? month = '0' + month : month
    var day = date.getDate()
    day < 10 ? day = '0' + day : day
    var h = date.getHours()
    h < 10 ? h = '0' + h : h
    var m = date.getMinutes()
    m < 10 ? m = '0' + m : m
    var time = year + '-' + month + '-' + day + ' ' + h + ':' + m
</script>